﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Http;
using Jw.Models;
using Jw.Core;
using Jw.Web.Areas.Mms.Common;
using System.Collections;
using Jw.Web.Areas.Pacm.Models;
using System.IO;
using Jw.Utils.NPOI.SS.UserModel;
using Jw.Utils.NPOI.HSSF.UserModel;
using Jw.Web.App_Start;

namespace Jw.Web.Areas.Pacm.Controllers
{
    public class XhjhController : BaseController
    {
        public void Down()
        {
            string infoId = "000006";
            IWorkbook hssfworkbook = null;
            r_Report obj = new r_ReportService().GetModel(ParamQuery.Instance().AndWhere("Report_Id", "000006"));
            var valueService = new r_ZbsjService();
            using (MemoryStream ms = new MemoryStream(obj.Report_ExportTemplete))
            {
                hssfworkbook = new HSSFWorkbook(ms);
                ISheet sheet = hssfworkbook.GetSheetAt(0);
                for (int i = 0; i <= sheet.LastRowNum; i++)
                {
                    IRow row = sheet.GetRow(i);
                    for (int j = 0; j < row.Cells.Count; j++)
                    {
                        ICell cell = row.Cells[j];
                        if (cell.StringCellValue.Contains("Calc"))
                        {
                            var valueModel = valueService.GetModel(ParamQuery.Instance().AndWhere("Zbsj_InfoID", infoId).AndWhere("Zbsj_Zbbh", cell.StringCellValue.Replace("[Calc]", "")));
                            if (valueModel != null)
                            {
                                cell.SetCellValue(valueModel.Zbsj_Value);
                            }
                            else
                            {
                                cell.SetCellValue("");
                            }
                        }
                    }

                };
                string fileName = "dayReport" + infoId + ".xls";
                string mapPath = Server.MapPath("").Substring(0, Server.MapPath("").IndexOf("pacm")) + "\\Areas\\pacm" + "\\download\\" + fileName;
                FileStream file = new FileStream(mapPath, FileMode.Create);
                hssfworkbook.Write(file);
                Jw.Utils.ZFiles.DownloadFile(System.Web.HttpContext.Current, file, "ss.xls", 1024102410);
                file.Close();
            }
        }
        // GET: /Pacm/Xhjh/
        public ActionResult Index()
        {
            //年份
            List<ComItem> comYearItems = new List<ComItem>();
            comYearItems.Add(new ComItem() { text = DateTime.Now.AddYears(-1).Year.ToString(), value = DateTime.Now.AddYears(-1).Year.ToString() });
            comYearItems.Add(new ComItem() { text = DateTime.Now.Year.ToString(), value = DateTime.Now.Year.ToString() });
            comYearItems.Add(new ComItem() { text = DateTime.Now.AddYears(1).Year.ToString(), value = DateTime.Now.AddYears(1).Year.ToString() });


            //月份
            List<ComItem> comMonthItems = new List<ComItem>();
            for (int i = 0; i < 12; i++)
            {
                ComItem obj = new ComItem();
                obj.text = obj.value = (i + 1).ToString("00");
                comMonthItems.Add(obj);
            }

            var model = new
            {
                dataSource = new
                {
                    yearsItems = comYearItems,
                    monthItems = comMonthItems
                },
                form = new
                {
                    Xhjh_Year = DateTime.Now.Date.Year,
                    Xhjh_Month = "",
                    Xhjh_UpdateDate = ""
                }

            };
            return View(model);
        }



        public ActionResult Edit(string id)
        {
            var userName = FormsAuth.GetUserData().UserName;
            var data = new XHJHApiController().GetEditMaster(id);
            var model = new
            {
                form = data.form,
                scrollKeys = data.scrollKeys,
                dataSource = new
                {
                },
                defaultForm = new p_Xhjh().Extend(new
                {
                    Xhjh_BillNo = id,
                    Xhjh_Year = DateTime.Now.Year.ToString(),
                    Xhjh_Month = DateTime.Now.Month.ToString("00"),
                    CreatePerson = userName,
                    CreateDate = DateTime.Now
                }),
                setting = new
                {
                    postFormKeys = new string[] { "Xhjh_BillNo" }
                }
            };

            return View(model);
        }

    }

    public class XHJHApiController : ApiController
    {




        public dynamic Get(RequestWrapper query)
        {
            query.LoadSettingXmlString(@"
            <settings defaultOrderBy='Xhjh_BillNo'>
                <select>
                   p_Xhjh.*
                </select>
                <from>
                    p_Xhjh
                </from>
                <where defaultForAll='true' defaultCp='equal' defaultIgnoreEmpty='true' >
                    <field name='Xhjh_BillNo'             cp='equal'     ></field>
                </where>
            </settings>
            ");
            var pQuery = query.ToParamQuery();
            var list = new p_XhjhService().GetDynamicListWithPaging(pQuery);
            return list;

        }
        public string GetNewBillNo()
        {
            return new p_XhjhService().GetNewKey("Xhjh_BillNo", "dateplus");
        }
        public dynamic GetEditMaster(string id)
        {
            p_Xhjh obj = new p_XhjhService().GetModel(ParamQuery.Instance().AndWhere("Xhjh_BillNo", id));
            return new
            {
                form = obj,
                scrollKeys = new p_XhjhService().ScrollKeys("Xhjh_BillNo", id.ToString())
            };
        }
        public void Edit(dynamic data)
        {
            //   <columns ignore = 'Xhjh_BillNo'></columns>
            var formWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>{0}</table>
                <where><field name='Xhjh_BillNo' cp='equal'></field></where>
            </settings>", typeof(p_Xhjh).Name);

            var result = new p_XhjhService().Edit(formWrapper, null, data);
        }

        public void Delete(string id)
        {
            var result = new p_XhjhService().Delete(ParamDelete.Instance().AndWhere("Xhjh_BillNo", id));
            MmsHelper.ThrowHttpExceptionWhen(result <= 0, "单据删除失败[Xhjh_BillNo={0}]，请重试或联系管理员！", id);
        }

    }

}
